package com.elite.groovy.callback

import com.elite.groovy.dev.WebSocketClient
import com.elite.groovy.kh.LogToDB
import com.elite.groovy.util.EliteBean
import com.elite.groovy.util.Util
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.text.SimpleDateFormat

/**
 * @projectName: ngsapi_cs
 * @package: cn.com.callback
 * @className: apiDataExport
 * @author: LiJT
 * @description: 数据导出服务
 * @date: 2022-12-19 14:03
 * @version: 1.0
 */

public class apiCallWebSocket implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiCallWebSocket.class);

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");

        String callBody = ""; // 入参请求体
        JSONObject resJson = JSONObject.fromObject("{\"code\":-1,\"msg\":\"\"}");

        try {

            // 调用类型校验,此接口仅支持GET
            String getMethod = request.getMethod();
            log.debug("getMethod:" + getMethod);
            if (!getMethod.equals("POST")) {
                log.error("HTTP请求调用类型错误，接口调用失败：" + getMethod);
                resJson.put("msg", "HTTP请求调用类型错误，接口调用失败：" + getMethod);
                return resJson;
            }

            // 获取所有Header
            Map<String, String> mapHeader = new HashMap<String, String>();
            Enumeration headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String key = (String) headerNames.nextElement();
                String value = request.getHeader(key);
                log.debug("Call Header-" + key + ":" + value);

                mapHeader.put(key, value);
            }

            // 获取所有参数名
            Map<String, String> mapParams = new HashMap<String, String>();
            Enumeration ep = request.getParameterNames();
            while (ep.hasMoreElements()) {
                String key = (String) ep.nextElement();
                String value = request.getParameter(key);
                log.debug("Call Parameter-" + key + ":" + value);
                mapParams.put(key, value);

            }

            // 获取调用数据
            callBody = Util.getRequestBody(request, "UTF-8");
            log.info("接收调用 POST 参数信息：" + callBody);

            JSONObject socketJson = JSONObject.fromObject(callBody);
            WebSocketClient.pushMessage("ws://127.0.0.1:9100/ngs/ws", socketJson.toString());
            resJson.put("code", 1);
            resJson.put("msg", "");

        } catch (Exception e) {
            log.error("全局处理异常:" + e.getMessage());
            resJson.put("msg", "全局处理异常:" + e.getMessage());
        } finally {
            LogToDB.toDB(eliteBean, "dataSource", "apiCallWebSocket[*]", "apiCallWebSocket", callBody, resJson.toString(), "本次发送总耗时：" + (System.currentTimeMillis() - startTime).toString(), "WS");

        }

        return resJson;

    }

}